<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
  <wx-tables
    :columns="columns"
    title="商务合作"
    :data-source="dataSource"
    :loading="loading"
    :total="total"
    :size.sync="size"
    :page.sync="page"
    @handlePage="handlePage"
  >
    <!-- tools -->
    <template #tools>
      <el-button type="primary" class="table-btn" @click="handleOpenForm()">新增</el-button>
      <!--      <el-button class="table-btn" icon="el-icon-refresh-right" @click="handleGetTable">刷新</el-button>-->
    </template>
    <template v-slot:status="record">
      <el-switch
        v-model="record.row.status"
        :active-value="1"
        :inactive-value="0"
        active-color="#409EFF"
        inactive-color="#C0CCDA"
        @change="handleSetStatus(record.row)">
      </el-switch>
    </template>
    <template v-slot:oper="record">
      <div class="oper-box">
        <span><el-link type="primary" @click="handleEit(record.row)">编辑</el-link></span>
<!--        <span><el-link type="primary" @click="handleChangePassWord(record.row.id)">重置密码</el-link></span>-->
        <el-popconfirm
          title="确定删除当前账号吗？"
          @confirm="handleDelete(record.row.id)"
        >
          <el-link slot="reference" type="danger">删除</el-link>
        </el-popconfirm>
      </div>
    </template>
    <wx-dialog
      ref="wx-dialog"
      :visible.sync="formVisible"
      :is-add="isAdd"
      width="500px"
      @ok="handleSaveForm"
    >
      <div class="wx-dialog-form">
        <el-form ref="ruleForm" v-loading="formLoading" :rules="ruleForm" :model="formInfo" label-position="left" label-width="90px">
          <el-form-item label="名称" prop="name">
            <el-input v-model="formInfo.name" style="width: 200px;" placeholder="请输入名称" />
          </el-form-item>
          <el-form-item label="联系电话" prop="tel">
            <el-input v-model="formInfo.tel" style="width: 200px;" placeholder="请输入联系电话" />
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="formInfo.email" style="width: 200px;" placeholder="请输入邮箱" />
          </el-form-item>
        </el-form>
      </div>
    </wx-dialog>
  </wx-tables>
</template>

<script>
import WxDialog from '@/components/WxDialog'
import WxTables from '@/components/WxTables'
import TableMixins from '@/components/WxTables/mixins'
import {
  sysCooperationCreate,
  sysCooperationDestroy,
  sysCooperationEdit,
  sysCooperationGetListByCon
} from '@/api/cooperation'

var validPhone = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请输入电话号码'))
  } else if (!/^1[3|4|5|7|8][0-9]\d{8}$/.test(value)) {
    callback(new Error('请输入正确的11位手机号码'))
  } else {
    callback()
  }
}

export default {
  name: 'ManagerList',
  components: {
    WxDialog,
    WxTables
  },
  mixins: [TableMixins],
  data() {
    return {
      pwdDialog: false,
      columns: [
        { config: { type: 'index', width: 100 }, label: '序号' },
        { dataIndex: 'name', label: '名称' },
        { dataIndex: 'tel', label: '联系电话'},
        { dataIndex: 'email', label: '邮箱' },
        // { dataIndex: 'status', label: '是否启用', slots: true },
        { dataIndex: 'oper', label: '操作', slots: true }
      ],
      markId: '',
      town_id: '',
      ruleForm: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        tel: [
          { required: true, trigger: 'blur', validator: validPhone }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ]
      },
      formInfo: {}
    }
  },
  methods: {
    /**
     * 获取列表
     */
    getList() {
      sysCooperationGetListByCon({town_id:this.town_id,page: this.page}).then(res => {
        if (this.pagination) {
          const { data, total } = res.data
          this.total = total
          this.dataSource = data
          this.cacheDataSource = JSON.parse(JSON.stringify(data))
        } else {
          this.dataSource = res.data
          this.cacheDataSource = JSON.parse(JSON.stringify(res.data))
        }
        this.handleSetCancelStatus?.()
        // this.handleCompleteTable()
      }).finally(() => {
        this.loading = false
      })
    },
    /**
     * 新增/编辑人员
     */
    createManager(idSend) {
      if(Object.keys(idSend).length) {
        sysCooperationEdit({ ...this.formInfo,...idSend,town_id: this.town_id })
          .then((res) => {
            if (res.code === 200) {
              this.$message.success(this.isAdd ? '添加成功' : '编辑成功')
              this.formInfo = {}
              this.formVisible = false
              this.handleGetTable()
            }
          }).finally(() => {
          this.handleResetDialog()
        })
      }else{
        sysCooperationCreate({ ...this.formInfo,town_id: this.town_id })
          .then((res) => {
            if (res.code === 200) {
              this.$message.success(this.isAdd ? '添加成功' : '编辑成功')
              this.formInfo = {}
              this.formVisible = false
              this.handleGetTable()
            }
          }).finally(() => {
          this.handleResetDialog()
        })
      }
    },
    /**
     * 更改人员状态
     */
    changeStatus(id,status) {
      managerSetStatus({
        admin_town_id: id,
        status
      }).then((res) => {
        if (res.code === 200) {
          this.$message.success(res.msg)
          this.handleGetTable()
        }
      })
    },
    /**
     * 重置人员密码
     * @param id
     */
    handleChangePassWord(id) {
      adminPortalResetPassword({ id,type:2 }).then((res) => { if (res.code === 200) { this.$message.success(res.msg) } })
    },
    /**
     * 编辑
     * @param item
     */
    handleEit(item) {
      const { id, name, tel, email } = item
      this.markId = 'sys_cooperation_id'
      this.handleOpenForm(id,['name','tel', 'email'],{name,tel,email})
    },
    /**
     * 删除管理员
     * @param admin_ids
     */
    handleDelete(sys_cooperation_ids) {
      sysCooperationDestroy({ sys_cooperation_ids }).then((res) => { if (res.code === 200) {
        this.$message.success(res.msg)
        this.handleGetTable()
      } })
    },
    /**
     * 翻页
     * @param val
     */
    handlePage(val) {
      this.page = val
    }
  }
}
</script>
<style lang="scss" src="./style/index.scss" scoped/>
